<template>
  <div class="learn-manage">
    <el-card class="manage-card">
      <template #header>
        <div class="card-header">
          <span>学习资料管理</span>
          <el-button type="primary" @click="showAddDialog">添加资料</el-button>
        </div>
      </template>
      
      <el-table :data="learningMaterials" style="width: 100%">
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="type" label="类型" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="studentCount" label="学习人数" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加资料' : '编辑资料'"
      width="50%"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="文档" value="document" />
            <el-option label="视频" value="video" />
            <el-option label="链接" value="link" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" type="textarea" rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

interface LearningMaterial {
  id: string
  title: string
  type: string
  content: string
  createTime: string
  studentCount: number
}

const learningMaterials = ref<LearningMaterial[]>([])
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const form = ref({
  id: '',
  title: '',
  type: '',
  content: ''
})

// 显示添加对话框
const showAddDialog = () => {
  dialogType.value = 'add'
  form.value = {
    id: '',
    title: '',
    type: '',
    content: ''
  }
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row: LearningMaterial) => {
  dialogType.value = 'edit'
  form.value = { ...row }
  dialogVisible.value = true
}

// 处理删除
const handleDelete = (_row: LearningMaterial) => {
  ElMessageBox.confirm('确定要删除该资料吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // TODO: 调用删除API
    ElMessage.success('删除成功')
  })
}

// 处理提交
const handleSubmit = () => {
  // TODO: 调用添加/编辑API
  ElMessage.success(dialogType.value === 'add' ? '添加成功' : '编辑成功')
  dialogVisible.value = false
}

onMounted(() => {
  // TODO: 从API获取学习资料列表
  learningMaterials.value = [
    {
      id: '1',
      title: '示例资料',
      type: '文档',
      content: '示例内容',
      createTime: '2024-04-21',
      studentCount: 10
    }
  ]
})
</script>

<style scoped>
.learn-manage {
  padding: 20px;
}

.manage-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>